
***********************************************************************
** BUILD OUT SENSITIVITY 
***********************************************************************
//initialize functions to check policies on a certain variable 
clear
include "../Data_Cleaning/Policy_Simulation_Programs.do"



local fulllist isNew isExpansion isMiningProj isPowerProj isMountaintopProj isMountaintopComp isHighCarbonProj isNewCustomer ///
isProjFin isThermal hasNoDecarbonStrat isMiningCo isPowerCo isCorpFin CoalFracRevParent CoalSharePowerParent


tempfile time
clear 
set obs 22
gen year = 2008+_n
save `time'


use ../Raw/Bank_Sample/BankSample_List, clear 
cross using `time'
tempfile banktrack 
save `banktrack'

foreach var in `fulllist' {
	di "`var'"
	qui policy_sim_univariate `var'
	merge 1:1 BankID year using `banktrack', keep(2 3) nogen
	order vdiff, after(n_ijg_trans)
	gen `var' = abs(vdiff) > 0 & ~missing(vdiff)
	rename vdiff `var'_frac
	save `banktrack', replace
}
foreach var in `fulllist' {
	di "`var'"
	rename `var'_frac frac_sens_`var'
	rename `var' is_sens_`var'
}
drop source BankName n_ds_trans n_ijg_trans 


save ../Intermediate/policy_sensitivities_yearly, replace


***********************************************************************
** BUILD OUT SENSITIVITY WITH INTIAL POLCIES ONLY
***********************************************************************
//initialize functions to check policies on a certain variable 
clear
include "../Data_Cleaning/Policy_Simulation_Programs.do"



local fulllist isNew isExpansion isMiningProj isPowerProj isMountaintopProj isMountaintopComp isHighCarbonProj isNewCustomer ///
isProjFin isThermal hasNoDecarbonStrat isMiningCo isPowerCo isCorpFin CoalFracRevParent CoalSharePowerParent


tempfile time
clear 
set obs 22
gen year = 2008+_n
save `time'


use ../Raw/Bank_Sample/BankSample_List, clear 
cross using `time'
tempfile banktrack 
save `banktrack'

foreach var in `fulllist' {
	di "`var'"
	qui policy_sim_univariate_min `var'
	merge 1:1 BankID year using `banktrack', keep(2 3) nogen
	order vdiff, after(n_ijg_trans)
	gen `var' = abs(vdiff) > 0 & ~missing(vdiff)
	rename vdiff `var'_frac
	save `banktrack', replace
}
foreach var in `fulllist' {
	di "`var'"
	rename `var'_frac i_frac_sens_`var'
	rename `var' i_is_sens_`var'
}
drop source BankName n_ds_trans n_ijg_trans  

save ../Intermediate/policy_sensitivities_yearly_initial, replace




***********************************************************************
** BUILD OUT YEARLY POLICY INTENSITY
***********************************************************************
//get policy strength by bank-year via unweighted simulation
policy_sim_strength
sum ban_intensity
replace ban_intensity = ban_intensity/r(max)
keep BankID year ban_intensity 
compress
save ../Intermediate/policy_intensity_yearly, replace


***********************************************************************
** RUN CODE TO GENERATE STRENGTH MEASURE BASED ON 16 SCENARIOS 
***********************************************************************
include "../Data_Cleaning/Build_Policy_Strength_Alternative_Measurement.do"

***********************************************************************
** COMBINE STUFF INTO A BAN STRENGTH DATASET 
***********************************************************************
use ../Raw/Bank_Sample/BankSample_List, clear 

recast str80 BankName, force
merge m:1 BankName using ../Intermediate/policy_strength_reclaim.dta, keep(1 3) nogen
merge m:1 BankName using ../Intermediate/sbti_clean.dta, nogen
merge 1:m BankID using ../Intermediate/policy_sensitivities_yearly, keep(3) nogen 
merge 1:1 BankID year using ../Intermediate/policy_sensitivities_yearly_initial, keep(3) nogen 
merge 1:1 BankID year using ../Intermediate/policy_intensity_yearly, keep(1 3)
gen has_coded_policy = _merge == 3
drop _merge


merge 1:1 BankID year using ../Intermediate/policy_intensity_yearly_new, keep(1 3) 
gen has_coded_policy_new1 = _merge == 3
drop _merge

gen sbti_panel=(year_sbti>=year & !missing(year_sbti))

gen ba15_panel=sbti_panel*ba15_d
replace ba15_panel=0 if missing(ba15_panel)
gen netzero_panel=sbti_panel*netzero_d
replace netzero_panel=0 if missing(netzero_panel)


//get first year the bank has a policy with ANY bite
gen fy = year if ban_intensity > 0   
egen first_ban_year = min(fy), by(BankID) 
drop fy  
replace first_ban_year = . if has_coded_policy == 0  
gen active_policy = year >= first_ban_year

gen fy = year if ban_intensity_new1 > 0   
egen first_ban_year_new1 = min(fy), by(BankID) 
drop fy  
replace first_ban_year_new1 = . if has_coded_policy_new1 == 0  
gen active_policy_new1 = year >= first_ban_year_new1
compress

egen reclaim_max=rowmax(developers projects relativethreshold phaseout absolutethreshold)
egen reclaim_average=rowtotal(developers projects relativethreshold phaseout absolutethreshold)
replace reclaim_average=reclaim_average/5


**build time-varying reclaim indices
gen rf_max=active_policy*reclaim_max
gen rf_average=active_policy*reclaim_average
gen rf_phaseout = active_policy*phaseout 
gen rf_dev = active_policy*developers 
gen rf_proj = active_policy*projects
egen n_intensity = nvals(ban_intensity), by(BankID)
replace n_intensity = n_intensity - 1 
bys BankID: gen firstB = _n == 1
tab n_intensity if firstB == 1
drop firstB

gen phaseup_ap = active_policy*n_intensity

save ../Intermediate/bank_ban_strength_yearly, replace





